layui.use(['table', 'laydate', 'jquery', 'form'], () => {
    let table = layui.table,
        laydate = layui.laydate,
        $ = layui.jquery;
    let renderIdx = 0;
    dict = getDict();
    let cols = [[
        {type: 'checkbox', fixed: 'left'}
        , {field: 'id', title: 'ID', fixed: 'left', unresize: true, sort: true}
        , {field: 'name', title: '姓名', align: 'center'}
        , {field: 'age', title: '年龄', sort: true, align: 'center'}
        , {
            field: 'sex', title: '性别', sort: true, align: 'center', templet: (e) => {
                return dict.sex[e.sex];
            }
        }
        , {field: 'teamName', title: '小组', align: 'center'}
        , {field: 'phone', title: '手机', align: 'center'}
        , {
            field: 'status', title: '状态', sort: true, align: 'center', templet: (e) => {
                return dict.status[e.status];
            }
        }
        , {
            title: '签到状态', align: 'center', width: 300, templet: (e) => {
                return `<input type="radio" name="signStatus` + (++renderIdx) + `" value="arrived" title="已到" checked>
                        <input type="radio" name="signStatus` + renderIdx + `" value="leave" title="请假"/>
                        <input type="radio" name="signStatus` + renderIdx + `" value="late" title="迟到">`;
            }
        }
        , {
            title: '迟到时间', align: 'center', width: 200, templet: (e) => {
                return `<input type="text" class="layui-input lated lateTime` + renderIdx + `"  disabled/>`;
            }
        }
    ]];

    /* 获取登录用户信息并渲染根据teamId渲染签到页面 */
    getAdmin(signMemberRender);


    /* 成员签到表格渲染 */
    function signMemberRender(data) {
        render("memberTable", {
            "query": "/admin/sign/notSignList/".concat(isEmpty(data.teamId) ? -1 : data.teamId),
        }, "成员签到", cols, ['40%', '72%'], {"done": signControlRender});
    }

    /* 控件事件渲染 */
    function signControlRender(e) {
        if ($(".lateTime1").length !== 0) {
            for (let i = 1; i <= e.data.length; i++) {
                //签到状态单选框选择器
                let selector = "input[name='signStatus" + i + "']";
                //绑定点击事件,点击时勾选行选中框
                $(selector).next().bind("click", (e) => {
                    let dataIndex = $(e.currentTarget).parent().parent().parent().attr("data-index");
                    let preCheckBox = $(".layui-table-fixed").find("tr[data-index='" + dataIndex + "']").find(".layui-form-checkbox");
                    if (!$(preCheckBox).hasClass("layui-form-checked")) {
                        $(preCheckBox).click();
                    }
                    //点击迟到单选按钮时,渲染时间组件
                    if ($(e.currentTarget).prev().val() === "late") {
                        laydate.render({
                            elem: ".lateTime".concat(i)
                            , type: 'datetime'
                            , trigger: 'focus'
                            , closeStop: ".lateTime".concat(i)
                        });
                        layer.open({
                            type: 2,
                            title: '',
                            area: ["0.1%", "0.1%"],
                            success: (layero, idx) => {
                                layer.close(idx);
                                $(".lateTime".concat(i)).attr("disabled", false).focus();
                            }
                        });
                    }
                });
            }
        }

        table.on('toolbar(memberTable)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            let data = checkStatus.data;
            switch (obj.event) {
                case 'submit':
                    if (data.length === 0) {
                        return layer.msg("当前未选中");
                    }
                    // layuiTable的行索引
                    let dataIndex = [];
                    let idTds = $(".layui-table-fixed").find("td[data-field='id']");
                    for (let td of idTds) {
                        for (let item of data) {
                            if ($(td).text() === item.id.toString()) {
                                dataIndex.push($(td).parent().attr("data-index"));
                            }
                        }
                    }
                    // 请求参数
                    let params = [];
                    for (let i = 0; i < dataIndex.length; i++) {
                        let sign = {};
                        let trSelector = ".layui-table-main tr[data-index=" + dataIndex[i] + "]";
                        let signStatus = $(trSelector).find(".layui-form-radioed").prev().val();
                        sign["userId"] = data[i].id;
                        sign["signStatus"] = signStatus;
                        sign["signTime"] = dateFormat("yyyy-MM-dd HH:mm:ss", new Date());
                        sign["latedTime"] = $(trSelector).find(".lated").val();
                        if (signStatus !== "arrived") {
                            sign.signTime = null;
                        } else if (signStatus !== "late") {
                            sign.latedTime = null;
                        }
                        params.push(sign);
                    }
                    ajax("/admin/sign/signIn", "POST", params, (res) => {
                        layer.msg(res.msg);
                        if (res.code === 0) {
                            table.reload("memberTable");
                        }
                    });
                    break;
            }
        });
    }
});
